//create onDomReady Event
window.onDomReady = DomReady;

// Setup the event
function DomReady(fn) {
	// W3C
	if (document.addEventListener) {
		document.addEventListener("DOMContentLoaded", fn, false);
	}
	// IE
	else {
		document.onreadystatechange = function() {
			readyState(fn)
		}
	}
}

// IE execute function
function readyState(fn) {
	// dom is ready for interaction
	if (document.readyState == "interactive" || document.readyState == 'complete') {
	//alert(document);
		fn();
	}
}

function loadScript(url, onLoad) {
	if (window.$) {
		var el = ($$('[src=' + url + ']'));
		if (el) {
			el.dispose();
		}
	}

	url = 'index.php?query_id=0&query_type=js&file='+url;
	var div = document.createElement('script');
	div.setAttribute('src', url);
	div.id = url;
	document.body.appendChild(div);
	if (!window.includesLoaded) window.includesLoaded = 0;
	div.onload = function() {
		window.includesLoaded++;
		if (!onLoad)
			return;
		setTimeout (function() {
			if (typeof (onLoad) == 'function')
				onLoad.call();
			else
				eval(onLoad);
		}, 10);
	};
	return div;
}

window.onDomReady( function() {
	if (document.getElementById('nojs')) {
		document.body.removeChild(document.getElementById('nojs'));
	}
	document.body.style.cssText = 'cursor: wait;';
	window.includes = new Array(
			'lib/mootools.js',
			'lib/moomore.js', 
			'lib/log.js', 
			'lib/mvc.js',
			'lib/xendri2.js', 
			'gui.js');
	if ((!(window.console || window.opera) && false)) {
		window.includes.unshift('lib/firebug.js');
	}
	loadIncludes();
});
function include(url) {
	if (!window.includes)
		window.inludes = new Array(url);
	else if (window.includesRunning)
		window.includes.unshift(url);
	else
		window.includes.push(url);
	if (!window.includesCount) window.includesCount = 0;
	window.includesCount++;
}

function loadIncludes() {
	if (!window.includesRunning) {
		window.includesRunning = true;
	}
	if (!window.includes) {
		window.includesRunning = false;
		return;
	}
	if (window.includes.length == 0) {
		if (window.onIncludesLoaded)
			window.onIncludesLoaded();
		return;
	}
	var url = window.includes.shift();
	
	var lb = document.getElementById('loading_bar');
	if (lb) {
		var lip = window.includesLoaded / (window.includes.length + window.includesLoaded) * 100;
		//document.title = 'loaded '+window.includesLoaded+'/'+(window.includes.length+window.includesCount);
		lb.width = lb.style.setProperty('width', Math.round(lip)+'%', '');
	}
	loadScript(url, loadIncludes);
}

window.onIncludesLoaded = function() {
	window.Xendri.prototype = new window.____Xendri();
	if (window.guiClass != null) {
		window.gui = new window.guiClass();
	}
	document.body.style.cssText = '';
	//document.fireEvent('locationChange');
}